package com.example.mallone11.Consumer.handler;


import com.example.mallone11.Consumer.pojo.Consumer;
import com.example.mallone11.Consumer.service.ConsumerService;
import com.example.mallone11.merchant.pojo.Goods;
import com.example.mallone11.merchant.service.MerchantService;
import com.example.mallone11.utils.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("consumer")
public class ConsumerHandle {
    @Resource
    private ConsumerService consumerService;

    //注册商家的service
    @Resource
    private MerchantService merchantService;

    @RequestMapping("add")
    public R register(@RequestParam("account")String account, @RequestParam("passwd")String passwd) {
        Consumer consumer = consumerService.findByAccount(account);
        if (consumer != null) {
            return R.fail("账号已经被注册");
        }
        return consumerService.register(account,passwd)>0 ? R.ok("注册成功") : R.fail("注册失败");

    }
    @PostMapping("login")
    public R login(@RequestParam("account")String account,@RequestParam("passwd")String passwd){
        Consumer consumer=consumerService.login(account,passwd);
        return consumer != null ? R.ok("登陆成功") : R.fail("账号密码错误");
    }
    @RequestMapping("findAll")
    public R findAll(){
        List<Goods> allGoods =merchantService.findAllGood();
        return allGoods.size() > 0 ? R.ok(allGoods) : R.fail("没有商品");
    }
}
